﻿Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml

Module mXMLConfig
    Public listen_host As String = "localhost"
    Public listen_port As Integer = 7777
    Public max_connection As Integer = 100
    Public auto_start As Boolean = False

    ''''' FOR CLIENT CONFIGURATION STORE
    Public Function reconfig_database(db_host As String, db_port As String, db_user As String, db_password As String, db_name As String)
        Return New Object() {db_host, db_port, db_user, db_password, db_name}
    End Function

    Public Function reconfig_server(listen_host As String, listen_port As String, max_connection As String, auto_start As String)
        Return New Object() {listen_host, listen_port, max_connection, auto_start}
    End Function


    Public Sub client_write_configuration()
        Dim path As String = My.Application.Info.DirectoryPath.ToString & "\server.xml"
        Dim xml_writer As New XmlTextWriter(path, System.Text.Encoding.UTF8)
        Dim xmldoc As New XmlDocument()

        ' Encryptor
        Dim sharedkey As New TripleDESCryptoServiceProvider()
        Dim md5 As New MD5CryptoServiceProvider()
        Dim ed As New EncryptedData()
        Dim exml As EncryptedXml
        Dim encryptElement As XmlElement
        Dim encryptXML As Byte()

        xml_writer.WriteStartDocument(True)
        xml_writer.Formatting = Formatting.Indented
        xml_writer.Indentation = 2
        xml_writer.WriteStartElement("Configuration")
        xml_writer.WriteStartAttribute("version")
        xml_writer.WriteString("1.0")
        xml_writer.WriteEndAttribute()

        xml_writer.WriteStartElement("Server")
        xml_writer.WriteStartAttribute("listen_ip")
        xml_writer.WriteString("localhost")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("listen_port")
        xml_writer.WriteString("7777")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("auto_connect")
        xml_writer.WriteString("0")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("max_connection")
        xml_writer.WriteString("0")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteEndElement()

        xml_writer.WriteStartElement("Database")
        xml_writer.WriteStartAttribute("host")
        xml_writer.WriteString("localhost")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("port")
        xml_writer.WriteString("3306")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("username")
        xml_writer.WriteString("root")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("password")
        xml_writer.WriteString("")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteStartAttribute("database")
        xml_writer.WriteString("db_mserver")
        xml_writer.WriteEndAttribute()
        xml_writer.WriteEndElement()

        xml_writer.WriteEndElement()
        xml_writer.WriteEndDocument()
        xml_writer.Close()

        xmldoc.Load(path)


        sharedkey.Key = md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes("PM0C9988HAX7585"))

        exml = New EncryptedXml(xmldoc)
        encryptElement = CType(xmldoc.SelectSingleNode("/Configuration"), XmlElement)
        encryptXML = exml.EncryptData(encryptElement, sharedkey, False)

        ed.Type = EncryptedXml.XmlEncElementUrl
        ed.EncryptionMethod = New EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl)
        ed.CipherData = New CipherData()
        ed.CipherData.CipherValue = encryptXML

        EncryptedXml.ReplaceElement(encryptElement, ed, False)

        xmldoc.Save(path)
    End Sub

    Public Sub client_update_configuration(ByVal Application() As Object, ByVal Server() As Object, ByVal Database() As Object)
        Try
            Dim path As String = My.Application.Info.DirectoryPath.ToString & "\server.xml"

            Dim sharedkey As New TripleDESCryptoServiceProvider()
            Dim md5 As New MD5CryptoServiceProvider()
            Dim xmlDocument As New XmlDocument()
            Dim ed As New EncryptedData()
            Dim encryptedXML As New EncryptedXml()
            Dim xmlElement As XmlElement
            Dim decryptedXML As Byte()

            sharedkey.Key = md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes("PM0C9988HAX7585"))

            xmlDocument.Load(path)
            xmlElement = CType(xmlDocument.GetElementsByTagName("EncryptedData")(0), XmlElement)
            ed.LoadXml(xmlElement)

            decryptedXML = encryptedXML.DecryptData(ed, sharedkey)
            encryptedXML.ReplaceData(xmlElement, decryptedXML)

            Dim xmlnode As XmlNodeList
            xmlnode = xmlDocument.GetElementsByTagName("Configuration")

            ' Load Server Config
            If IsArray(Server) And Server.Length = 4 Then
                xmlnode(0).ChildNodes(0).Attributes.ItemOf("listen_ip").InnerText = Server(0)
                xmlnode(0).ChildNodes(0).Attributes.ItemOf("listen_port").InnerText = Server(1)
                xmlnode(0).ChildNodes(0).Attributes.ItemOf("auto_connect").InnerText = Server(2)
                xmlnode(0).ChildNodes(0).Attributes.ItemOf("max_connection").InnerText = LCase(Server(3))
            End If

            ' Load Database Config
            If IsArray(Database) And Database.Length = 5 Then
                xmlnode(0).ChildNodes(1).Attributes.ItemOf("host").InnerText = Database(0)
                xmlnode(0).ChildNodes(1).Attributes.ItemOf("port").InnerText = Database(1)
                xmlnode(0).ChildNodes(1).Attributes.ItemOf("username").InnerText = Database(2)
                xmlnode(0).ChildNodes(1).Attributes.ItemOf("password").InnerText = Database(3)
                xmlnode(0).ChildNodes(1).Attributes.ItemOf("database").InnerText = Database(4)
            End If

            encryptedXML = New EncryptedXml(xmlDocument)
            xmlElement = CType(xmlDocument.SelectSingleNode("/Configuration"), XmlElement)
            decryptedXML = encryptedXML.EncryptData(xmlElement, sharedkey, False)

            ed.Type = encryptedXML.XmlEncElementUrl
            ed.EncryptionMethod = New EncryptionMethod(encryptedXML.XmlEncTripleDESUrl)
            ed.CipherData = New CipherData()
            ed.CipherData.CipherValue = decryptedXML

            encryptedXML.ReplaceElement(xmlElement, ed, False)

            xmlDocument.Save(path)
        Catch ex As Exception

        End Try
    End Sub

    Public Sub client_read_configuration()
        Try
            Dim path As String = My.Application.Info.DirectoryPath.ToString & "\server.xml"

            Dim sharedkey As New TripleDESCryptoServiceProvider()
            Dim md5 As New MD5CryptoServiceProvider()
            Dim encryptedDoc As New XmlDocument()
            Dim ed As New EncryptedData()
            Dim encryptXML As New EncryptedXml()
            Dim EncryptedElement As XmlElement
            Dim decryptedXML As Byte()

            sharedkey.Key = md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes("PM0C9988HAX7585"))

            encryptedDoc.Load(path)
            EncryptedElement = CType(encryptedDoc.GetElementsByTagName("EncryptedData")(0), XmlElement)
            ed.LoadXml(EncryptedElement)

            decryptedXML = encryptXML.DecryptData(ed, sharedkey)
            encryptXML.ReplaceData(EncryptedElement, decryptedXML)

            Dim xmlnode As XmlNodeList
            xmlnode = encryptedDoc.GetElementsByTagName("Configuration")
            ' Load Server Config
            listen_host = xmlnode(0).ChildNodes(0).Attributes.ItemOf("listen_ip").InnerText
            listen_port = XmlConvert.ToInt32(xmlnode(0).ChildNodes(0).Attributes.ItemOf("listen_port").InnerText)
            auto_start = XmlConvert.ToBoolean(xmlnode(0).ChildNodes(0).Attributes.ItemOf("auto_connect").InnerText)
            max_connection = XmlConvert.ToInt32(xmlnode(0).ChildNodes(0).Attributes.ItemOf("max_connection").InnerText)

            ' Load Database Config
            dbHost = xmlnode(0).ChildNodes(1).Attributes.ItemOf("host").InnerText
            dbPort = Convert.ToInt32(xmlnode(0).ChildNodes(1).Attributes.ItemOf("port").InnerText)
            dbUser = xmlnode(0).ChildNodes(1).Attributes.ItemOf("username").InnerText
            dbPass = xmlnode(0).ChildNodes(1).Attributes.ItemOf("password").InnerText
            dbName = xmlnode(0).ChildNodes(1).Attributes.ItemOf("database").InnerText
        Catch ex As Exception
            client_write_configuration()
        End Try
    End Sub
End Module
